home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 26 Feb 1996 03:46:14 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Feb25204614@qcd.lanl.gov>
- References: <4gqpa1$3h9@alcor.usc.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: wawda@alcor.usc.edu's message of 25 Feb 1996 14:53:53 -0800
-
- In article <4gqpa1$3h9@alcor.usc.edu>
- wawda@alcor.usc.edu (Abu Wawda) writes:
-
- AW: I'm having trouble understanding what the address of a static array
- AW: is. For example, if I declare a variable called myarray as:
- AW: char myarray[10];
- AW: then what could &myarray possibly mean? myarray is not a pointer, so
- AW: &myarray could not possibly be the address of the variable myarray
- AW: (like it would be if I did char* myarray and then asked for &myarray).
-
- If you wrote int i, what does &i mean? Will you argue that it could
- not possibly be a pointer to the variable i because i is not apointer
- :-)?
-
- &myarray is a pointer to the array myarray. It is likely to have the
- same `value' as &myarray[0], but arithmetic on it works
- differently. Thus &myarray[0]+1 would skip over one char (i.e. it
- would be &myarray[1]), but &myarray+1 would skip over the entire array
- and hence point just beyond the entire array.
-
- It has absolutely nothing to do with char**.
-
- AW:
- AW: Functions such as scanf() allow the following:
- AW:
- AW: char myarray[10];
- AW:
- AW: scanf("%s",&myarray);
- AW:
- AW: but I don't understand what scanf() could possibly be taking in the
-
- `allow' is a dangerous word. %s must be given a char*, not a
- char(*)[10] as you have done. This causes undefined behaviour, and the
- compiler can do anything it likes, including making it seem like it
- works.
-
- (By the way, the standard has a garbled wording either in the scanf
- or the printf description of %s, I forget which. I am ignoring that.)
-
- AW: second parameter. It can't be: char** since myarray is not a
- AW: pointer. I CAN understand how the following would work:
- AW:
- AW: char* myarray;
- AW:
- AW: myarray = (char*) malloc(10);
- AW: scanf("%s",&myarray);
- AW:
- AW: Then scanf() is simply taking a char** (pointer to a character
- AW: pointer, or in other words the address of the pointer myarray) in its
-
- And again, that leads to undefined behaviour.
-
- AW: second paremeter. However, if I write my own function like this:
- AW:
- AW: void func(char** p)
- AW: {
- AW: // do something here
- AW: }
- AW:
- AW: I cannot pass &myarray if I declare it as: char myarray[10]. So how do
- AW: this work? Thanks in advance,
-
- I do not know what you mean to ask in this question.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-